<?php
	namespace Common\Logic;
	use Common\Model\CouponsModel;
	class CouponsLogic extends CouponsModel{
		/*
		* Function:		getCouponsList
		* Description:	优惠券列表列表
		* @param:		$page string 起始页
						$filter array 过滤数组
						$pageSize string 每页显示条数
		*/
		public function getCouponsList($page=1,$filter = '',$pageSize=20){
			$conditon = $this->getCouponsFilter($filter);
			return $this->where($conditon)->order('id desc')->limit(($page-1)*$pageSize,$pageSize)->select();
		}
		
		/*
		* Function:get:	getCouponsFilter
		* Description:	优惠券列表列表搜索过滤项
		* @param:		$filter array 过滤数组
		*/
		public function getCouponsFilter($filter = ''){
    		$conditon = array();
            $conditon['status'] = 1;
    		if(!empty($filter['title'])){
    			$conditon['title'] = array('like','%'.$filter['title'].'%');
    		}
            if(!empty($filter['cate'])){
                $conditon['cate'] = $filter['cate'];
            }
            if(!empty($filter['end_time'])){
                $conditon['validity_end_time'] = ['egt',$filter['end_time']];
            }
			return $conditon;
		}
		
		/*
		* Function:get:	getCouponsCount
		* Description:	优惠券列表列表总数
		* @param:		$filter array 过滤数组
		*/
		public function getCouponsCount($filter = ''){
			$conditon = $this->getCouponsFilter($filter);
			return $this->where($conditon)->count();
		}
		
		/*
		* Function:		addCoupons
		* Description:	增加优惠券列表
		* @param:		$data array 增加的数组
		*/
		public function addCoupons($data){
			return $this->add($data);
		}
		
		/*
		* Function:		getCouponsInfo
		* Description:	获取优惠券列表信息 by row
		* @param:		$id int ID
		*/
		public function getCouponsInfo($id){
			return $this->where(array('id'=>$id))->find();
		}
		
		/*
		* Function:		updateCoupons
		* Description:	更新优惠券列表
		* @param:		$data array 更新的数组
		*/
		public function updateCoupons($data){
			return $this->save($data);
		}
		
		/*
		* Function:		deleteCoupons
		* Description:	删除优惠券列表
		* @param:		$id int ID
		*/
		public function deleteCoupons($id){
			return $this->where(array('id'=>$id))->delete();
		}
		
		/*
		* Function:		getCouponsAllList
		* Description:	部门列表
		*/
		public function getCouponsAllList(){
			return $this->where(array('status'=>1))->select();
		}
	}
?>